/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IO;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;


/**
 *
 * @author ndnguyen
 */
public class Exam {
    private int totalQuestion = 0;
    private String title="";
    private String description = "";
    private String length = "";
    private List<Question> questionList = new ArrayList<Question>();



    /**
     * started or not, default false
     */
    public boolean isActive = false;

    /**
     * stopped or not, default true
     */
    public boolean stop = true;
    private int submitted;

    public String getTitle()
    {
        return title;
    }

    public String getDescription()
    {
        return description;
    }

    public Question getQuestion(int i)
    {
	return questionList.get(i);
    }
    public String getLength()
    {
	return length;
    }

    public int getTotalQuestion()
    {
        return totalQuestion;
    }

    public void addOneQuestion(String i, String s, List<String> listChoice)
    {
		Question newQuestion = new Question(i, s, listChoice);
		questionList.add((Integer.parseInt(i)-1), newQuestion);
		setTotalQuestion(totalQuestion + 1);
    }

    public Exam()
    {
        
    }

    public Exam(String filename) throws ParserConfigurationException
    {
        try {
                DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = db.parse(new File(filename));
                
                NodeList nodeList = doc.getElementsByTagName("title");
                Element element = (Element)nodeList.item(0);
                String tit = element.getChildNodes().item(0).getNodeValue();
                tit = tit.trim();
                this.title = tit;
               

                nodeList = doc.getElementsByTagName("description");
                element = (Element)nodeList.item(0);
                String descrip = element.getChildNodes().item(0).getNodeValue();
                descrip = descrip.trim();
                this.description = descrip;

                nodeList = doc.getElementsByTagName("length");
                element = (Element)nodeList.item(0);
                String len = element.getChildNodes().item(0).getNodeValue();
                len = len.trim();
                this.length = len;

                System.out.println(tit);
                System.out.println(descrip);
                System.out.println(len);

                nodeList = doc.getElementsByTagName("question");

                    for (int i = 0; i < nodeList.getLength(); i++)
                    {
                        element = (Element)nodeList.item(i);

                        NodeList nodeList2 = element.getElementsByTagName("number");
                        Element element2 = (Element)nodeList2.item(0);
                        String number = element2.getChildNodes().item(0).getNodeValue();

                        nodeList2 = element.getElementsByTagName("statement");
                        element2 = (Element)nodeList2.item(0);
                        String statement = element2.getChildNodes().item(0).getNodeValue();
                        statement = statement.trim();

                        System.out.println(number);
                        System.out.println(statement);

                        nodeList2 = element.getElementsByTagName("choice");
                        List<String> choices = new ArrayList<String>();

                        for (int j = 0; j < nodeList2.getLength(); j++)
                        {
                            element2 = (Element)nodeList2.item(j);
                            String choice = element2.getChildNodes().item(0).getNodeValue();
                            choice = choice.trim();
                            choices.add(j, choice);
                            System.out.println(choice);
                        }
                       this.addOneQuestion(number,statement,choices);
                    }
		}
                catch (ParserConfigurationException pce)
                {
			System.err.println("Can not parse ");
		} 
                catch (SAXException se)
                {
			System.err.println("Wrong format!");
		}
                catch (IOException ioe)
                {
			System.err.println("Cannot read");
		}
		
    }
    public static void main(String[] args) throws ParserConfigurationException
    {
        Exam xml = new Exam("Test.xml");
        System.out.println("\n\n ^^ start : ");
        System.out.println(xml.getLength());
        System.out.println(xml.getQuestion(1).getStatement());
    }

    /**
     * @param totalQuestion the totalQuestion to set
     */
    public void setTotalQuestion(int totalQuestion) {
        this.totalQuestion = totalQuestion;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param length the length to set
     */
    public void setLength(String length) {
        this.length = length;
    }

    /**
     * @return the submitted
     */
    public int getSubmitted() {
        return submitted;
    }

    /**
     * @param submitted the submitted to set
     */
    public void setSubmitted(int submitted) {
        this.submitted = submitted;
    }

    public void startClient()
    {
        
    }

}
